# -*- coding: utf-8 -*-
# @Time    : 2020-10-19 13:55
# @Author  : Daniel

import random
import string
import argparse


class PASSWORD(object):
    def __init__(self, length, allow_special=False):
        if length > 0:
            self.length = length
        else:
            self.length = 8
            print('输入长度 %d 非法, 启用默认值 8' % length)
        self.allow_special = allow_special

    def create_password(self):
        if self.allow_special:
            result = ''.join(random.sample(string.ascii_letters + string.digits + string.punctuation, self.length))
        else:
            result = ''.join(random.sample(string.ascii_letters + string.digits, self.length))

        return result


if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--length", type=int, default=8, help='定义密码长度，正整数')
    parser.add_argument("-s", "--special", choices=['y', 'n'], default='y', help='是否允许特殊字符， y/n')

    args = parser.parse_args()

    p = PASSWORD(args.length, args.special.lower() == 'y')
    print(p.create_password())
